package com.hy.main.demo.io;

import org.junit.Test;

import java.io.*;

/**
 * @Author shy Boy
 * @Date 2022/4/26 - 04 - 26 - 21:33
 * @Description: com.hy.main.demo.file
 * @version: 1.0
 */
public class BufferedCopy {

    @Test
    public void copyFile() throws IOException {
        String copyPath = "e:\\myFile.txt";
        String path = "d:\\fundInfo.txt";
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(copyPath));
            bufferedWriter = new BufferedWriter(new FileWriter(path));
            String lineData;
            // line按行读取
            while ((lineData = bufferedReader.readLine()) != null) {
                bufferedWriter.write(lineData);
                // 虽然是按行读取 但是readLine并没有提供换行符 所以要自己手动添加
                bufferedWriter.newLine();

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                bufferedReader.close();
            }

            if (bufferedWriter != null) {
                bufferedWriter.close();
            }
        }
    }
}
